<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: Stream iteration pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.3.1</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Stream iteration pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>stream iteration</b> pattern allows loops in data stream processing. An operation is applied to a data item until a predicate is satisfied. When the predicate is met, the result is sent to the output stream. This streaming pattern can only be used inside another pattern and consequently does not take an execution policy itself, but uses the execution policy of its enclosing pattern.</p>
<p>The interface to the <b>stream iteration</b> pattern is provided by function <code><a class="el" href="namespacegrppi.html#a7854f699ca4ad6933101813c245e605b">grppi::repeat_until()</a></code>.</p>
<div class="fragment"><div class="line"><a class="code" href="group__pipeline__pattern.html#gacb5843d0d5fd08d02fa4679b81b9d918">grppi::pipeline</a>(ex,</div><div class="line">  stage1,</div><div class="line">  <a class="code" href="namespacegrppi.html#a7854f699ca4ad6933101813c245e605b">grppi::repeat_until</a>(arguments...),</div><div class="line">  stage2,</div><div class="line">  stage3,</div><div class="line">  ...);</div></div><!-- fragment --><h2>Stream iteration variants</h2>
<p>There is a single variant:</p>
<ul>
<li><em>Composable stream iteration</em>: Defines a stream iteration that can be used as a building block by another pattern (e.g. pipeline).</li>
</ul>
<h2>Key elements in stream iteration</h2>
<p>The key elements in a <b>stream iteration</b> are a <b>Transformer</b> used to transform data items, and a <b>Predicate</b> that defines when the iteration should finish.</p>
<p>The <b>Transformer</b> may be any C++ callable entity that takes a data item and applies a transformation to it. Thus, a <b>Transformer</b> <code>op</code> is any operation that, given an input value <code>x</code> of type <code>T</code>makes valid the following:</p>
<div class="fragment"><div class="line">U res{transformer(x)};</div></div><!-- fragment --><p>The <b>Predicate</b> may be any C++ callable entity that takes a data item and returns a value that is contextually convertible to <code>bool</code>. Thus, a predicate <code>pred</code> is any operation, that given a value <code>x</code> of type <code>T</code>, makes the following valid:</p>
<div class="fragment"><div class="line"><span class="keywordflow">do</span> { <span class="comment">/*...*/</span> } <span class="keywordflow">while</span> (!predicate(item));</div></div><!-- fragment --><h2>Details on stream iteration variants</h2>
<h3>Stand-alone stream iteration</h3>
<p>A composable <b>stream iteration</b> has two elements:</p>
<ul>
<li>A <b>Transformer</b> of values.</li>
<li>A <b>Predicate</b> for terminating the iteration. <hr/>
 <b>Example</b>: For every natural number x, print the first value x*2^n that is greater than 1024. <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;grppi::pipeline(ex</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;  [i=0,max=100]() mutable -&gt; optional&lt;int&gt; {</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;    if (i&lt;max) return i++;</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;    else return {};</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;  },</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;  grppi::repeat_until(</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;    [](int x) { return 2*x; },</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;    [](int x) { return x&gt;1024; }),</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;  [](int x) { cout &lt;&lt; x &lt;&lt; endl; }</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;);</div></div><!-- fragment --><hr/>
</li>
</ul>
<p>For composing complex patterns, the <code><a class="el" href="namespacegrppi.html#a7854f699ca4ad6933101813c245e605b">repeat_until()</a></code> function may be used to create an object that may be used later in the composition. </p><hr/>
<p> <b>Example</b>: For every natural number x, print the first value x*2^n that is greater than 1024. </p><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;auto loop = grppi::repeat_until(</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;    [](int x) { return 2*x; },</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;    [](int x) { return x&gt;1024; });</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;grppi::pipeline(ex</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;  [i=0,max=100]() mutable -&gt; optional&lt;int&gt; {</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;    if (i&lt;max) return i++;</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;    else return {};</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;  },</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;  loop,</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;  [](int x) { cout &lt;&lt; x &lt;&lt; endl; }</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;);</div></div><!-- fragment --><hr/>
 </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
